$(function () {
    // 渲染页面——后面还要用，所以封装成函数
    // 封装
    initArtCateList();
    let layer = layui.layer
    function initArtCateList() {
        $.ajax({
            url: "/my/article/cates",
            success: function (res) {
                console.log(res);
                // 判断
                if (res.status != 0) {
                    return layer.msg(res.message)
                }
                let str = template("tpl-art-cate", res);
                $("tbody").html(str);
            }
        })
    }

    // 2.显示添加文章分类列表
    $("#btnAdd").on("click", function () {
        indexadd = layer.open({
            type: 1,
            title: '请输入文章分类',
            area: ["500px", "250px"],
            content: $("#dialog-add").html()
        });
    })

    // 3.提交文章分类添加、利用事件委托形式
    let indexadd = null;
    $("body").on("submit", "#form-add", function (e) {
        e.preventDefault();
        $.ajax({
            method: "POST",
            url: "/my/article/addcates",
            data: $(this).serialize(),
            success: function (res) {
                if (res.status != 0) {
                    return layer.msg(res.message);

                }
                initArtCateList();
                layer.msg("恭喜您，文章类别增加成功！");
                layer.close(indexadd);

            }
        })
    })

    let indexEdit = null
    let form = layui.form;
    $("tbody").on("click", ".btn-edit", function () {
        indexEdit = layer.open({
            type: 1,
            title: '修改文章分类',
            area: ["500px", "260px"],
            content: $("#dialog-edit").html()
        });
        // 获取id发送ajax渲染页面
        let Id = $(this).attr("data-id");
        $.ajax({
            method: "GET",
            url: "/my/article/cates/" + Id,
            success: function (res) {
                form.val("form-edit", res.data)
            }
        })
    })

    // 修改
    $("body").on("submit", "#form-edit", function (e) {
        e.preventDefault();
        $.ajax({
            method: "POST",
            url: "/my/article/updatecate",
            data: $(this).serialize(),
            success: function (res) {
                if (res.status != 0) {
                    return layer.msg(res.message)
                }
                initArtCateList();
                layer.msg("恭喜您，文章类别修改成功！")
                layer.close(indexEdit)
            }
        })
    })


    // 删除
    $("body").on("click", ".btn-delete", function () {
        let Id = $(this).attr("data-id");
        layer.confirm("是否确认删除", { icon: 3, title: "提示" },
            function (index) {
                $.ajax({
                    method: "GET",
                    url: "/my/article/deletecate/" + Id,
                    success: function (res) {
                        if (res.status != 0) {
                            return layer.msg(res.message)
                        }
                        initArtCateList();
                        layer.msg("恭喜您，删除成功！");
                        layer.close(indexEdit);
                    }
                })
            })
    })








})
